package com.xoa.controller.cms;


import com.xoa.service.cms.core.CmsPublisher;
import com.xoa.service.cms.core.FileUtils;
import com.xoa.service.cms.core.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by cyt on 2018/7/7.
 */
@Controller
@RequestMapping("/cmsPub")
public class CmsPubController {
    @Autowired
    private CmsPublisher publisher;

    @RequestMapping("/portal")
    public void pub(HttpServletRequest request, HttpServletResponse response,boolean previewFlag){
        int siteId = FileUtils.getInteger(request.getParameter("siteId"), 0);
        int channelId = FileUtils.getInteger(request.getParameter("channelId"), 0);
        int chnlDocId = FileUtils.getInteger(request.getParameter("chnlDocId"), 0);
        Map params = RequestUtils.getParamMap(request);//额外请求参数

        try (PrintWriter writer = response.getWriter();){
            response.setContentType("text/html;charset=utf-8");
            if(siteId!=0 && channelId==0 && chnlDocId==0){//渲染首页模板
                String model=publisher.renderSiteIndexPage(request,response,siteId,params, previewFlag);
                if(model!=null) {
                    writer.write(model);
                }else{}
            }else if(siteId!=0 && channelId!=0 && chnlDocId==0){//渲染栏目首页模板
                writer.write(publisher.renderChannelIndexPage(request,channelId,params, previewFlag));
            }else if(siteId!=0 && channelId!=0 && chnlDocId!=0){//渲染栏目文档模板
                writer.write(publisher.renderDocPage(request,chnlDocId,params, previewFlag));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
